package com.lxw.nonghe.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.lxw.nonghe.annotation.RequiresPermission;
import com.lxw.nonghe.common.Result;
import com.lxw.nonghe.entity.ChronicDiseaseCategory;
import com.lxw.nonghe.service.ChronicDiseaseCategoryService;
import com.lxw.nonghe.util.LogUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Date;

/**
 * 慢性病分类表
 */
@RestController
@RequestMapping("/chronicDiseaseCategory")
@RequiresPermission("chronicDiseaseCategory:manage")
public class ChronicDiseaseCategoryController {
    @Autowired
    private ChronicDiseaseCategoryService chronicDiseaseCategoryService;
    @Autowired
    private LogUtils logUtils;

    @PostMapping("/create")
    public Result<Boolean> addChronicDiseaseCategory(@RequestBody ChronicDiseaseCategory chronicDiseaseCategory) {
        if (chronicDiseaseCategory == null) {
            logUtils.recordLog("慢性病分类管理", "新增慢性病分类", "失败", "慢性病分类对象不能为空");
            return Result.error("慢性病分类对象不能为空");
        }
        try {
            // 设置创建时间和更新时间
            Date now = new Date();
            chronicDiseaseCategory.setCreateTime(now);
            chronicDiseaseCategory.setUpdateTime(now);

            boolean result = chronicDiseaseCategoryService.save(chronicDiseaseCategory);
            if (result) {
                ChronicDiseaseCategory savedCategory = chronicDiseaseCategoryService.getDiseaseByName(chronicDiseaseCategory.getDiseaseName());
                logUtils.recordLog("慢性病分类管理", "新增慢性病分类", "成功",
                        "疾病名称:" + savedCategory.getDiseaseName() + ", 疾病ID:" + savedCategory.getDiseaseId());
                return Result.success("慢性病分类新增成功", true);
            } else {
                logUtils.recordLog("慢性病分类管理", "新增慢性病分类", "失败", "保存数据库失败");
                return Result.error("慢性病分类新增失败");
            }
        } catch (Exception e) {
            logUtils.recordLog("慢性病分类管理", "新增慢性病分类", "失败", e.getMessage());
            return Result.error("系统异常: " + e.getMessage());
        }
    }

    @PostMapping("/delete/{id}")
    public Result<Boolean> deleteChronicDiseaseCategory(@PathVariable Integer id) {
        if (id == null) {
            logUtils.recordLog("慢性病分类管理", "删除慢性病分类", "失败", "疾病ID不能为空");
            return Result.error("疾病ID不能为空");
        }
        try {
            ChronicDiseaseCategory chronicDiseaseCategory = chronicDiseaseCategoryService.getById(id);
            boolean result = chronicDiseaseCategoryService.removeById(id);
            if (result) {
                logUtils.recordLog("慢性病分类管理", "删除慢性病分类", "成功",
                        "疾病ID:" + id + ", 疾病名称:" + (chronicDiseaseCategory != null? chronicDiseaseCategory.getDiseaseName() : "未知"));
                return Result.success("慢性病分类删除成功", true);
            } else {
                logUtils.recordLog("慢性病分类管理", "删除慢性病分类", "失败", "疾病ID:" + id + "不存在");
                return Result.error("慢性病分类删除失败");
            }
        } catch (Exception e) {
            logUtils.recordLog("慢性病分类管理", "删除慢性病分类", "失败", e.getMessage());
            return Result.error("系统异常: " + e.getMessage());
        }
    }

    @PostMapping("/update")
    public Result<Boolean> updateChronicDiseaseCategory(@RequestBody ChronicDiseaseCategory chronicDiseaseCategory) {
        if (chronicDiseaseCategory == null || chronicDiseaseCategory.getDiseaseId() == null) {
            logUtils.recordLog("慢性病分类管理", "更新慢性病分类", "失败", "慢性病分类对象或疾病ID不能为空");
            return Result.error("慢性病分类对象或疾病ID不能为空");
        }
        try {
            // 无论前端是否传递 updateTime，都设置为当前时间
            chronicDiseaseCategory.setUpdateTime(new Date());

            boolean result = chronicDiseaseCategoryService.updateById(chronicDiseaseCategory);
            if (result) {
                logUtils.recordLog("慢性病分类管理", "更新慢性病分类", "成功",
                        "疾病ID:" + chronicDiseaseCategory.getDiseaseId());
                return Result.success("慢性病分类更新成功", true);
            } else {
                logUtils.recordLog("慢性病分类管理", "更新慢性病分类", "失败",
                        "疾病ID:" + chronicDiseaseCategory.getDiseaseId() + "不存在");
                return Result.error("慢性病分类更新失败");
            }
        } catch (Exception e) {
            logUtils.recordLog("慢性病分类管理", "更新慢性病分类", "失败", e.getMessage());
            return Result.error("系统异常: " + e.getMessage());
        }
    }

    @GetMapping("/get/{id}")
    public Result<ChronicDiseaseCategory> getChronicDiseaseCategoryById(@PathVariable Integer id) {
        if (id == null) {
            logUtils.recordLog("慢性病分类管理", "查询慢性病分类详情", "失败", "疾病ID不能为空");
            return Result.error("疾病ID不能为空");
        }
        try {
            ChronicDiseaseCategory chronicDiseaseCategory = chronicDiseaseCategoryService.getById(id);
            if (chronicDiseaseCategory != null) {
                return Result.success("慢性病分类查询成功", chronicDiseaseCategory);
            } else {
                logUtils.recordLog("慢性病分类管理", "查询慢性病分类详情", "失败", "疾病ID:" + id + "不存在");
                return Result.error("未找到该慢性病分类");
            }
        } catch (Exception e) {
            logUtils.recordLog("慢性病分类管理", "查询慢性病分类详情", "失败", e.getMessage());
            return Result.error("查询异常: " + e.getMessage());
        }
    }

    @GetMapping("/list")
    public Result<Page<ChronicDiseaseCategory>> getChronicDiseaseCategoryPage(
            @RequestParam(defaultValue = "1") int current,
            @RequestParam(defaultValue = "10") int size,
            @RequestParam(required = false) String diseaseName) {
        if (current < 1) {
            current = 1;
        }
        if (size < 1 || size > 100) {
            size = 10;
        }
        try {
            Page<ChronicDiseaseCategory> chronicDiseaseCategoryPage = chronicDiseaseCategoryService.getDiseasePage(current, size, diseaseName);
            return Result.success("慢性病分类分页查询成功", chronicDiseaseCategoryPage);
        } catch (Exception e) {
            logUtils.recordLog("慢性病分类管理", "分页查询慢性病分类", "失败", e.getMessage());
            return Result.error("分页查询异常: " + e.getMessage());
        }
    }

    @PostMapping("/query")
    public Result<Page<ChronicDiseaseCategory>> queryChronicDiseaseCategories(
            @RequestBody ChronicDiseaseCategory queryParam,
            @RequestParam(defaultValue = "1") int current,
            @RequestParam(defaultValue = "10") int size) {
        if (current < 1) {
            current = 1;
        }
        if (size < 1 || size > 100) {
            size = 10;
        }
        try {
            Page<ChronicDiseaseCategory> chronicDiseaseCategoryPage = chronicDiseaseCategoryService.queryDisease(queryParam, current, size);
            return Result.success("慢性病分类条件查询成功", chronicDiseaseCategoryPage);
        } catch (Exception e) {
            logUtils.recordLog("慢性病分类管理", "条件查询慢性病分类", "失败", e.getMessage());
            return Result.error("条件查询异常: " + e.getMessage());
        }
    }
}
